/**@author: Administrator
 * @date: 2013-2-25
 * @Description:
 */
package org.benxiong.iplay.ui;

import java.io.File;
import java.io.FileNotFoundException;

import org.benxiong.iplay.R;
import org.benxiong.iplay.http.request.RequestFactory;
import org.benxiong.iplay.util.GlobalVar;
import org.benxiong.iplay.util.ImageUtil;
import org.benxiong.iplay.util.LogX;
import org.benxiong.iplay.util.PathUtil;
import org.benxiong.iplay.util.PreferencesUtil;
import org.benxiong.iplay.util.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @author Administrator
 *
 */
public class HomeActivity extends BaseActivity{
	

	String	DEBUG_TAG	= "iPlay";

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int action = MotionEventCompat.getActionMasked(event);

		switch (action) {
		case (MotionEvent.ACTION_DOWN):
			Log.d(DEBUG_TAG, "Action was DOWN");
			return true;
		case (MotionEvent.ACTION_MOVE):
			ToastUtil.showMessageOnExit("..................................", this);
			Log.d(DEBUG_TAG, "Action was MOVE");
			return true;
		case (MotionEvent.ACTION_UP):
			Log.d(DEBUG_TAG, "Action was UP");
			return true;
		case (MotionEvent.ACTION_CANCEL):
			Log.d(DEBUG_TAG, "Action was CANCEL");
			return true;
		case (MotionEvent.ACTION_OUTSIDE):
			Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
					"of current screen element");
			return true;
		default:
			return super.onTouchEvent(event);
		}
	}
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		GlobalVar.appContext = this;
		if (!PreferencesUtil.isLogin()) {
			finish();
			jumpToActivity(LoginActivity.class);
		} else {
			setContentView(R.layout.main);
		}
		
		GlobalVar.userName = PreferencesUtil.getLoginInfo().getString(PreferencesUtil.USERNAME, null);
		GlobalVar.password = PreferencesUtil.getLoginInfo().getString(PreferencesUtil.PASSWORD, null);
	}
	
	public void onResume() {
		super.onResume();
		
		//user has login, check user avator
		final File f = ImageUtil.getAvatorCache();
		if (f.exists()) {
			//check server profile user avator
			RequestParams rParam = new RequestParams();
			rParam.put("username", GlobalVar.userName);
			RequestFactory.getInstance().post(RequestFactory.URL_HAS_AVATOR, 
					rParam, new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(String arg0) {
							// TODO Auto-generated method stub
							super.onSuccess(arg0);
							LogX.print("========onSUccess=="+arg0);
							try {
								
								if (shouldProcess(arg0)) {
									JSONObject json = new JSONObject(arg0);
									boolean exists = json.getBoolean("data");
									if (exists) {
									LogX.print("===exists===");
									//delete local file
									} else {
										uploadAvator(f);
									}
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

						@Override
						public void onFailure(Throwable arg0) {
							// TODO Auto-generated method stub
							super.onFailure(arg0);
							LogX.print("========onFailure=="+arg0.getMessage());
						}
			});
		}
	}
	
	private void uploadAvator(File f) {
		//after upload success, delete avator file
		RequestParams rParam = new RequestParams();
		rParam.put("type", ""+PhotoManagerActivity.PHOTO_TYPE_AVATOR);
		try {
			rParam.put("file", f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		RequestFactory.getInstance().post(RequestFactory.URL_UPLOAD_PHOTO,
				rParam, new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						super.onFailure(arg0);
						LogX.print("====Pic=onFailure====" + arg0.getMessage());
					}

					@Override
					public void onSuccess(String arg0) {
						// TODO Auto-generated method stub
						super.onSuccess(arg0);
						LogX.print("===Pic==onSuccess====" + arg0);
					}
			
		});
		
		LogX.print("=====get User path URL ===="+PathUtil.profilePictureUrl(GlobalVar.userName, null));
		LogX.print("=====delete local file====");
		//f.delete();
	}

	/* (non-Javadoc)
	 * @see org.benxiong.iplay.ui.BaseActivity#initView()
	 */
	@Override
	protected void initView() {
		
	}

	/* (non-Javadoc)
	 * @see org.benxiong.iplay.ui.BaseActivity#onMessage(android.os.Message)
	 */
	@Override
	public void onMessage(Message msg) {
		
	}


	/* (non-Javadoc)
	 * @see org.benxiong.iplay.ui.BaseActivity#handleClick(int)
	 */
	@Override
	public void handleClick(int buttonId) {
		// TODO Auto-generated method stub
		
	}
	
	
}
